window.onload = function () {
    var inputData = [];
    var allData = [];
    var legendData = [];
    var seriesData = [];
    init(inputData);
    function init(data){
        $.ajax({
            type: "POST",
            url: "mortgagorAgeData",
            data:data,
            success: function (msg){
                if(msg.status=="false"){
                    return
                }else{
                    allData=msg.result;
                    allData.sort(function(a, b) {
                        // return a.ORDERNUMBE-b.ORDERNUMBE;
                        return a.ordernumbe-b.ordernumbe;
                    });
                    allData.forEach(function (data,index) {
                        // legendData[index] = data.SUBSECTION;
                        legendData[index] = data.subsection;
                        seriesData[index] = {
                            // name:data.SUBSECTION,
                            name:data.subsection,
                            // value:data.COUNT
                            value:data.count
                        }
                    });
                    doCharts();
                }
            }
        });
    }
    function doCharts(){
        var option4 = {
            title : {
                text: '',
                //subtext: '',
                x:'center'
            },
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                bottom:'bottom',
                //selectedMode:false,
                data: legendData,
                textStyle: {
                    color : "#ADADAD"
                },
            },
            series : [
                {
                    name: '年龄',
                    type: 'pie',
                    radius : '70%',
                    center: ['50%', '45%'],
                    data:seriesData,
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };

        var myChart = echarts.init($('#myChart4').get(0));
        myChart.setOption(option4);
        window.onresize = function () {
            myChart.resize()
        };
    }

    $('#searchData').click(function(){
        var citys = $('#city').val(),
            districts = $('#district').val(),
            loansBalanceStarts = $('#loansBalanceStart').val(),
            loansBalanceEnds =  $('#loansBalanceEnd').val(),
            loansAmountStarts = $('#loansAmountStart').val(),
            loansAmountEnds = $('#loansAmountEnd').val(),
            startDateStarts = $('#startDateStart').val(),
            startDateEnds = $('#startDateEnd').val(),
            loansDurations = $('#loansDuration').val(),
            risk = $('#risk').val(),
            buildAreaEnds = $('#buildAreaEnd').val(),
            buildAreaStarts = $('#buildAreaStart').val(),
            endDateStarts = $('#endDateStart').val(),
            endDateEnds = $('#endDateEnd').val(),
            inputData = {
                city:citys,
                district:districts,
                loansBalanceStart:loansBalanceStarts,
                loansBalanceEnd:loansBalanceEnds,
                loansAmountStart:loansAmountStarts,
                loansAmountEnd:loansAmountEnds,
                startDateStart:startDateStarts,
                startDateEnd:startDateEnds,
                loansDuration:loansDurations,
                risk:risk,
                buildAreaEnd:buildAreaEnds,
                buildAreaStart:buildAreaStarts,
                endDateStart:endDateStarts,
                endDateEnd:endDateEnds
            };
        init(inputData)
    });
};